# NOTICE: ARCH must be same as ARCH in $(EELDIR)/.tmp.config.mk if ARCH was defined
# ko install path: $(SHARE_DIR_ARCH)/$(KL_VER_DIR)/$(KL_CONFIG)/kmods/lib/modules/$(uname -r)/extra/$(kmod).ko

# can not define PWD at here
KMOD_DIR := $(firstword $(subst /linux-, ,$(PWD)))

$(if $(filter __%, $(MAKECMDGOALS)), \
	$(error targets prefixed with '__' are only for internal use))

# FIXME: $(KMOD_DIR)/scripts/config.mk include $(EELDIR)/toolchain.mk which define targets
#### default target ####
__PHONY += __all
__all: modules

# subarch.include copy from linux-6.6
include $(KMOD_DIR)/scripts/subarch.include
include $(KMOD_DIR)/scripts/config.mk

_srcs					:= $(wildcard $(PWD)/*.c)
_srcs					:= $(patsubst $(PWD)/%,%,$(_srcs))
srcs					?= $(filter-out $(kmod).mod.c,$(_srcs))

KM_PLAT					?=
kmod					?= $(shell echo $(notdir $(PWD)) | cut -d '-' -f 2)
$(kmod)-objs			?= $(patsubst %.c,%.o,$(srcs))
$(kmod)_$(KM_PLAT)		?= 1

# ignore $(PWD)/Makefile, and only show subdirs with depth less than 3
SUBDIRS := $(sort $(patsubst $(PWD)/%/Makefile,%,\
			$(shell find $(PWD) -mindepth 2 -maxdepth 3 -type f -name Makefile)))

ifneq ($(SUBDIRS),)
build_subdirs 			?= Y
else
MAKEFLAGS 				+= --no-print-directory
endif

ifneq ($(srcs),)
ifeq ($($(kmod)_$(KM_PLAT)),1)
build_module 			?= Y
endif
endif

ifeq ($(build_module),Y)
ifneq ($(wildcard $(kmod).c),)
$(error Please rename $(kmod).c)
endif
# obj-m can be customized by user, can not use ?=
ifeq ($(obj-m),)
obj-m 					:= $(kmod).o
endif
endif

#### basic targets ####
PHONY += modules modules_install clean
ifneq ($(build_module),Y)
modules modules_install clean:
	@:
else	# build_module is Y
LAST_MODULES			:= $(shell cat $(PWD)/.last_modules 2> /dev/null)
__PHONY += __check_clean
__check_clean:
	@:
ifneq ($(wildcard $(PWD)/.last_modules),)
# FIXME: error message
ifneq ($(LAST_MODULES),$(ARCH))
	@echo "cleaning last $(LAST_MODULES) modules build"
	$(EELQ4)$(MAKE) -f Makefile ARCH=$(LAST_MODULES) clean \
		|| $(MAKE) -f Makefile clean
endif # LAST_MODULES
endif # wildcard

$(kmod).ko:
	@(MAKE) -f Makefile modules

# basic depend
modules: __check_clean

# ARCH must be same as make modules when make modules_install
modules_install: $(kmod).ko

# ARCH is empty, build for host
ifeq ($(ARCH),)
modules modules_install:
	$(EELQ2)$(MAKE) $(JOB_OPT) $@ \
		-C $(S_KL_DIR) \
		M=$(PWD)
	$(EELQ5)echo -n > $(PWD)/.last_modules

clean:
	$(EELQ2)$(MAKE) $(JOB_OPT) clean \
		-C $(S_KL_DIR) \
		M=$(PWD)
	$(EELQ5)rm -f $(PWD)/.last_modules

else	# ARCH not empty
modules modules_install:
	$(EELQ3)mkdir -p $(SHARE_DIR_ARCH)/$(KL_VER_DIR)/$(KL_CONFIG)/kmods/
	$(EELQ2)$(MAKE) $(JOB_OPT) $@ \
		-C $(S_KL_DIR) \
		M=$(PWD) \
		ARCH=$(KL_ARCH) \
		CROSS_COMPILE=$(TC_PREFIX) \
		O=$(O_KL_DIR) \
		INSTALL_MOD_PATH=$(SHARE_DIR_ARCH)/$(KL_VER_DIR)/$(KL_CONFIG)/kmods
	$(EELQ5)echo -n $(ARCH) > $(PWD)/.last_modules

clean:
	$(EELQ3)mkdir -p $(SHARE_DIR_ARCH)/$(KL_VER_DIR)/$(KL_CONFIG)/kmods/
	$(EELQ2)$(MAKE) $(JOB_OPT) clean \
		-C $(S_KL_DIR) \
		M=$(PWD) \
		ARCH=$(KL_ARCH) \
		CROSS_COMPILE=$(TC_PREFIX) \
		O=$(O_KL_DIR) \
		INSTALL_MOD_PATH=$(SHARE_DIR_ARCH)/$(KL_VER_DIR)/$(KL_CONFIG)/kmods
	$(EELQ5)rm -f $(PWD)/.last_modules
endif	# end of ARCH
endif	# end of build_module
#### basic targets end ####

PHONY += help
help:
ifeq ($(build_module),Y)
	@echo basic targets:
	@echo "\t"help all modules modules_install clean
endif
ifeq ($(build_module)$(build_subdirs),YY)
	@echo
endif
ifeq ($(build_subdirs),Y)
	@echo subdir targets:
	@echo "\t"xxx-help xxx-all xxx-modules xxx-modules_install xxx-clean
	@echo subdirs:
	@for sub in $(SUBDIRS); do \
		echo "\t"$${sub}; \
	done
endif

#### subdir targets ####
# Usage: $(eval $(call subtarget,target))
define subtarget
SUBDIRS-$(1)			:= $$(addsuffix -$(1),$$(SUBDIRS))
PHONY += $$(SUBDIRS-$(1))
$$(SUBDIRS-$(1)):
	$(EELQ5)$(MAKE) $(1) \
		-C $$(patsubst %-$(1),%,$$@)
endef

ifeq ($(build_subdirs),Y)
PHONY += $(SUBDIRS)
$(SUBDIRS):
	$(EELQ5)$(MAKE) all-modules \
		-C $@
endif

$(eval $(call subtarget,modules))
$(eval $(call subtarget,modules_install))
$(eval $(call subtarget,clean))
#### subdir targets end ####

#### all targets ####
PHONY += all-modules all-modules_install all-clean
all-modules: 			modules $(SUBDIRS-modules)
all-modules_install: 	modules_install $(SUBDIRS-modules_install)
all-clean: 				clean $(SUBDIRS-clean)
#### all targets end ####

.PHONY: $(__PHONY) $(PHONY)
